package com.jaay.controller;


import com.alibaba.fastjson2.JSON;
import com.jaay.common.CommonResult;
import com.jaay.pojo.Order;
import com.jaay.service.OrderService;
import com.jaay.vo.OrderVo;
import dto.OrderDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @author chencanfeng
 */
@Slf4j
@RestController
public class OrderController {


    @Autowired
    private OrderService orderService;

    /**
     * 批量添加订单
     * @param orderList 订单列表
     * @return 结果信息
     */
    @PostMapping("/batchAddOrder")
    public CommonResult<String> addOrder(@RequestBody List<OrderVo> orderList) {

        log.info("创建订单:" + JSON.toJSONString(orderList));
        return orderService.batchAddOrder(orderList.stream()
                .map(orderVo -> {
                    Optional<OrderDto> optional = orderVo.copy();
                    return optional.orElse(new OrderDto());
                }).toList());
    }

    /**
     * 查询订单列表
     * @return 返回订单列表
     */
    @GetMapping("/queryOrderList")
    public CommonResult<List<Order>> queryOrderList() {

        return orderService.queryOrderAndProductList();
    }

}
